package com.fr.cell;

import com.fr.base.core.GraphHelper;
import com.fr.base.core.list.IntList;
import com.fr.cell.core.GridUtils;
import com.fr.report.Margin;
import com.fr.report.PaperSize;
import com.fr.report.Report;
import com.fr.report.ReportPageAttr;
import com.fr.report.ReportSettings;
import com.fr.report.TemplateReport;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.ReportHelper;
import com.fr.report.core.ReportUtils;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;

public class GridColumnUI extends ComponentUI
{
  private Color withoutDetailsBackground = Color.lightGray;

  public void paint(Graphics paramGraphics, JComponent paramJComponent)
  {
    if (!(paramJComponent instanceof GridColumn))
      throw new IllegalArgumentException("The component c to paint must be a GridColumn!");
    Graphics2D localGraphics2D1 = (Graphics2D)paramGraphics;
    GridColumn localGridColumn = (GridColumn)paramJComponent;
    ReportPane localReportPane = localGridColumn.getReportPane();
    Dimension localDimension = localGridColumn.getSize();
    Rectangle2D.Double localDouble = new Rectangle2D.Double(0.0D, 0.0D, localDimension.getWidth(), localDimension.getHeight());
    localGraphics2D1.setPaint(localGridColumn.getBackground());
    GraphHelper.fill(localGraphics2D1, localDouble);
    FontRenderContext localFontRenderContext = localGraphics2D1.getFontRenderContext();
    LineMetrics localLineMetrics = localGridColumn.getFont().getLineMetrics("", localFontRenderContext);
    float f = localLineMetrics.getAscent();
    TemplateReport localTemplateReport = localReportPane.getEditingReport();
    DynamicValueList localDynamicValueList = ReportHelper.getColumnWidthList(localTemplateReport);
    int i = localReportPane.getGrid().getHorizontalValue();
    int j = localReportPane.getGrid().getHorizontalBeginValue();
    j = i;
    localReportPane.getGrid().setHorizontalBeginValue(j);
    int k = localReportPane.getGrid().getHorizontalExtent();
    int l = localTemplateReport.getColumnCount() - 1;
    int i1 = i + k + 1;
    double d1 = localDimension.getWidth();
    if (!(localReportPane.isColumnEndless()))
    {
      i1 = Math.min(i1, l);
      d1 = localDynamicValueList.getRangeValue(i, Math.max(0, l + 1));
    }
    int i2 = localTemplateReport.getColumnCount();
    double d2 = 0.0D;
    if (i2 > j)
      d2 = localDynamicValueList.getRangeValue(j, i2);
    d2 = Math.min(d1, d2);
    if (localGridColumn.getBackground() != null)
    {
      localGraphics2D1.setPaint(this.withoutDetailsBackground);
      GraphHelper.fill(localGraphics2D1, new Rectangle2D.Double(0.0D, 0.0D, d2, localDimension.getHeight()));
      localGraphics2D1.setPaint(localGridColumn.getBackground());
      GraphHelper.fill(localGraphics2D1, new Rectangle2D.Double(d2, 0.0D, localDimension.getWidth() - d2, localDimension.getHeight()));
    }
    localGraphics2D1.setPaint(localGridColumn.getSeparatorLineColor());
    GraphHelper.drawLine(localGraphics2D1, 0.0D, 0.0D, 0.0D, localDimension.getHeight());
    double d3 = 0.0D;
    double d4 = 0.0D;
    double d5 = 0.0D;
    for (int i4 = 0; i4 <= i1; ++i4)
    {
      int i3;
      if (i4 == 0)
        i4 = j;
      d3 += d5;
      d5 = localDynamicValueList.get(i4);
      if (d5 <= 0.0D)
        d4 = d3 + 1.0D;
      else
        d4 = d3 + d5;
      GridSelection localGridSelection = localReportPane.getGridSelection();
      IntList localIntList = GridUtils.getGridSelectionColumnIndex(localGridSelection);
      int i5 = localGridSelection.getType();
      if ((i5 == 0) && (localIntList.contain(i4)))
      {
        localGraphics2D1.setColor(localGridColumn.getSelectedBackground());
        GraphHelper.fill(localGraphics2D1, new Rectangle2D.Double(d3 + 1.0D, 0.0D, d5 - 1.0D, localDimension.height));
        i3 = 1;
      }
      else
      {
        i3 = 0;
      }
      localGraphics2D1.setColor(localGridColumn.getSeparatorLineColor());
      GraphHelper.drawLine(localGraphics2D1, d4, 0.0D, d4, localDimension.height);
      Object localObject = localGridColumn.getGridCRRender().getDisplay(i4);
      if (localObject == null)
        return;
      if ((localObject instanceof String) || (localObject instanceof Number))
      {
        String str = localObject.toString();
        if (localTemplateReport.getReportPageAttr() != null)
        {
          if ((i4 >= localTemplateReport.getReportPageAttr().getRepeatHeaderColumnFrom()) && (i4 <= localTemplateReport.getReportPageAttr().getRepeatHeaderColumnTo()))
            str = str + "(HR)";
          if ((i4 >= localTemplateReport.getReportPageAttr().getRepeatFooterColumnFrom()) && (i4 <= localTemplateReport.getReportPageAttr().getRepeatFooterColumnTo()))
            str = str + "(FR)";
        }
        double d7 = localGridColumn.getFont().getStringBounds(str, localFontRenderContext).getWidth();
        if (d7 > d5)
        {
          Graphics2D localGraphics2D2 = (Graphics2D)localGraphics2D1.create();
          localGraphics2D2.setClip(new Rectangle2D.Double(0.0D, d3, localDimension.width, d5));
          if (i3 != 0)
            localGraphics2D2.setPaint(localGridColumn.getSelectedForeground());
          else if (localGridColumn.isEnabled())
            localGraphics2D2.setPaint(localGridColumn.getForeground());
          else
            localGraphics2D1.setPaint(UIManager.getColor("controlShadow"));
          GraphHelper.drawString(localGraphics2D2, str, d3 + (d5 - d7) / 2.0D, f + 2.0F + 1.0F);
          localGraphics2D2.dispose();
        }
        else
        {
          if (i3 != 0)
            localGraphics2D1.setPaint(localGridColumn.getSelectedForeground());
          else if (localGridColumn.isEnabled())
            localGraphics2D1.setPaint(localGridColumn.getForeground());
          else
            localGraphics2D1.setPaint(UIManager.getColor("controlShadow"));
          GraphHelper.drawString(localGraphics2D1, str, d3 + (d5 - d7) / 2.0D, f + 2.0F + 1.0F);
        }
      }
      else if (localObject instanceof Image)
      {
        GraphHelper.paintImage(localGraphics2D1, (int)d5, localDimension.height, (Image)localObject, null, 3, -1, -1);
      }
      double d6 = localDynamicValueList.getRangeValue(0, i4);
      double d8 = localDynamicValueList.getRangeValue(0, i4 + 1);
      ReportSettings localReportSettings = ReportUtils.getReportSettings(localTemplateReport);
      PaperSize localPaperSize = localReportSettings.getPaperSize();
      if (localPaperSize == null)
        localPaperSize = PaperSize.PAPERSIZE_A4;
      Margin localMargin = localReportSettings.getMargin();
      double d9 = 0.0D;
      if (localReportSettings.getOrientation() == 1)
        d9 = (localPaperSize.getHeight() - localMargin.getLeft() - localMargin.getRight()) * localReportSettings.getResolution();
      else
        d9 = (localPaperSize.getWidth() - localMargin.getLeft() - localMargin.getRight()) * localReportSettings.getResolution();
      int i6 = (int)(d6 / d9);
      int i7 = (int)(d8 / d9);
      if ((i7 - i6 == 1) && (i7 != 0))
      {
        localGraphics2D1.setPaint(localGridColumn.getSeparatorLineColor());
        double d10 = i7 * d9 - d6;
        GeneralPath localGeneralPath = new GeneralPath(0, 4);
        localGeneralPath.moveTo((float)(d10 + d3 - 4.0D), 0.0F);
        localGeneralPath.lineTo((float)(d10 + d3 + 4.0D), 0.0F);
        localGeneralPath.lineTo((float)(d10 + d3), 10.0F);
        localGeneralPath.closePath();
        localGraphics2D1.fill(localGeneralPath);
      }
    }
    localGraphics2D1.setColor(localGridColumn.getSeparatorLineColor());
    GraphHelper.drawLine(localGraphics2D1, 0.0D, 0.0D, d4, 0.0D);
  }
}